package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.app.qbake.R;
import com.app.qbake.module.mine.adapter.MyGoodsAdapter;
import com.app.qbake.module.mine.adapter.MyShelveGoodsAdapter;
import com.app.qbake.module.mine.di.MyGoodsListComponent;
import com.app.qbake.module.mine.event.EditGoodsSuccessEvent;
import com.app.qbake.module.mine.event.GoodsUpDownEvent;
import com.app.qbake.module.mine.mvp.MyGoodsListView;
import com.app.qbake.module.mine.presenter.MyGoodsListPresenter;
import com.wind.base.adapter.BaseDelegateRecyclerAdapter;
import com.wind.base.mvp.view.BasePageFragment;
import com.wind.base.request.BaseRequest;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.MyGoodsListRequest;
import com.wind.data.mine.MyGoodsListResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import javax.inject.Inject;

/**
 * Created by wind on 2018/10/19.
 */

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.MyGoodsListView",
                responseCanonicalName = "com.wind.data.mine.MyGoodsListResponse",
                requestCanonicalName = "com.wind.data.mine.MyGoodsListRequest",
                page = true

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "shop/getList")
)
public class MyGoodsListFragment extends BasePageFragment<MyGoodsListView, MyGoodsListPresenter, MyGoodsListComponent, MyGoodsListResponse>
        implements MyGoodsListView {
    private static final String ARGS_KEY_TYPE = "args_key_type";
    private String mType;
    @Inject
    MyGoodsListPresenter presenter;

    @Override
    public MyGoodsListPresenter createPresenter() {
        return presenter;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mType = getArguments().getString(ARGS_KEY_TYPE);
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_my_goods;
    }

    @Override
    protected BaseDelegateRecyclerAdapter getAdapter() {
        if (mType==MyGoodsListRequest.TYPE_PUB) {
            MyGoodsAdapter adapter = new MyGoodsAdapter(getActivity());
            return adapter;
        }else {
            MyShelveGoodsAdapter adapter=new MyShelveGoodsAdapter(getActivity());
            return adapter;
        }
    }

    @Override
    protected BaseRequest buildRequest(boolean isFirstPage) {
        MyGoodsListRequest request = new MyGoodsListRequest();
        request.setFirstPage(isFirstPage);
        request.setType(mType);
        return request;
    }

    public static Fragment getInstance(String type) {
        MyGoodsListFragment f = new MyGoodsListFragment();
        Bundle args = new Bundle();
        args.putString(ARGS_KEY_TYPE, type);
        f.setArguments(args);
        return f;
    }

    @Subscribe
    public void onGoodsUpDownEvent(GoodsUpDownEvent event){
        loadFirstPage();
    }


    /**
     * 编辑商品成功
     *
     * @param event
     */
    @Subscribe
    public void onEditGoodsSuccessEvent(EditGoodsSuccessEvent event) {
        loadFirstPage();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
